<?php

declare(strict_types=1);

namespace App\Helper;
use Hyperf\Context\Context;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\DbConnection\Db;
class Common
{
    /**
     * 获取客户端IP
     *
     * @return void
     */
    public static function getUserIp()
    {
        $request = Context::get(ServerRequestInterface::class);
        $res = $request->getServerParams();
        if(isset($res['http_client_ip'])){
            return $res['http_client_ip'];
        }elseif(isset($res['http_x_real_ip'])){
            return $res['http_x_real_ip'];
        }elseif(isset($res['http_x_forwarded_for'])){
            //部分CDN会获取多层代理IP，所以转成数组取第一个值
            $arr = explode(',',$res['http_x_forwarded_for']);
            return $arr[0];
        }else{
            return $res['remote_addr'];
        }
    }
    /**
     * 获取系统配置参数
     * [getSystemConf description]
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public static function getSystemConf($key)
    {
        $keys = [
            'color'=>['id','name','color'],
            'education'=>['id','name'],
            'nurse_level'=>['id','name'],
            'positional'=>['id','name'],
            'tbed'=>['id','name'],
            'tmod'=>['id','name'],
            'tunit'=>['id','name'],
        ];
        if(!array_key_exists($key, $keys)){
            return [];
        }
        $table = "conf_".$key;
        $datas = Db::table($table)
            ->select($keys[$key])
            ->where(['is_del'=>0,'status'=>1])
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 获取医院
     * [getHospitalByName description]
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    public static function getHospitalByName($name)
    {
        if(empty($name)){
            return [];
        }
        $datas = Db::table("conf_hospital")
            ->select(['id','name'])
            ->where([['name', 'like', '%'.$name.'%'],['is_del', '=', 0],['status', '=', 1]])
            ->orderBy('id', 'asc')
            ->offset(0)
            ->limit(10)
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 获取科室
     * [getOfficeConf description]
     * @return [type] [description]
     */
    public static function getOfficeConf()
    {
        $datas = [];
        $all = Db::table("conf_office")
            ->select(['id','name','pid'])
            ->where(['is_del'=>0,'status'=>1])
            ->get()
            ->toArray();
        foreach ($all as $key => $value) {
            if(empty($value['pid'])){
                $datas[$value['id']]['id'] = $value['id'];
                $datas[$value['id']]['name'] = $value['name'];
            }else{
                $datas[$value['pid']]['child'][] = [
                    'id'=>$value['id'],
                    'name'=>$value['name']
                ];
            }
        }
        $datas = array_values($datas);
        return $datas;
    }
}
